<?php

namespace App\Jobs\Shop;

use App\Lib\WeApp\WeApp;
use App\Models\Shop\WxShopOrder;
use App\Wen\Utils\Settings;
use App\Wen\Utils\ShopUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class ShopOrderDeliverInfoManage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $action = '';
    protected $shop_order_id = 0;
    protected $scanner = 0;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($action, $shop_order_id, $scanner)
    {
        //
        $this->action = $action;
        $this->scanner = $scanner;
        $this->shop_order_id = $shop_order_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $weapp = new WeApp('mini');
        $deliver_manage = $weapp->getDeliverInfoManage();
        if(Settings::get('need_mini_deliver_info_manage', 0) != 1){
            return;
        }
        if($this->shop_order_id > 0 && !_empty_($this->action)){
            $shopOrder = WxShopOrder::withTrashed()->find($this->shop_order_id);
            // 特定动作
            if($this->action == 'upload'){
                if(_empty_($this->scanner)){
                    return;
                }
                if($shopOrder->status == 1 && $shopOrder->pay_status == 2 && !_empty_($shopOrder->serial_number) && $shopOrder->serial_platform === 0 && $shopOrder->serial_platform_type == 'mini'){
                    $deliver_manage->selfPickUp($shopOrder, $this->scanner);
                }
            }else if($this->action == 'virtual-upload'){
                if($shopOrder->status == 9 && $shopOrder->pay_status == 2 && !_empty_($shopOrder->serial_number) && $shopOrder->serial_platform === 0 && $shopOrder->serial_platform_type == 'mini'){
                    $deliver_manage->virtualDeliverShopOrder($shopOrder);
                }
            }else if($this->action == 'query'){
                if(_empty_($this->scanner)){
                    return;
                }
            }else if($this->action == 'virtual-query'){
                if($shopOrder->status == 10 && !_empty_($shopOrder->serial_number) && $shopOrder->serial_platform === 0 && $shopOrder->serial_platform_type == 'mini'){
                    $wechat_order = $deliver_manage->getDeliverOrder($shopOrder->serial_number);
                    if($wechat_order){
                        if(in_array(_array_key($wechat_order, 'order_state', 0), [3,4])){
                            DB::beginTransaction();
                            try {
                                // todo:
                                $ship_at = date('Y-m-d H:i:s', time());
                                WxShopOrder::withTrashed()->where('id', $shopOrder->id)->update([
                                    'has_virtual' => 1,
                                    'ship_at' => $ship_at,
                                    'status' => 2,
                                ]);
                                ShopUtils::paied_content_process($shopOrder->id);

                                DB::commit();
                                return;
                            } catch (\Exception $e) {
                                DB::rollBack();
                                _logger_(__file__, __line__, $e->getMessage());
                                return;
                            }

                        }
                    }
                }
            }
        }
    }
}
